package com.wanxi.service.impl;

import com.wanxi.dao.CompanyDao;
import com.wanxi.dao.impl.CompanyDaoImpl;
import com.wanxi.dto.CompanyDTO;
import com.wanxi.entity.CompanyEntity;
import com.wanxi.service.CompanyService;
import com.wanxi.util.Tool;
import com.wanxi.vo.HomeVO;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements CompanyService {
    private CompanyDao companyDao = new CompanyDaoImpl();

    @Override
    public CompanyDTO find(HomeVO homeVO) {
        /**
         * 1.首先判断redis里面是否有数据
         * 2.如果有，则直接取出
         * 3.如果没有则查询数据库，然后将数据存入redis
         */
        Jedis jedis = new Jedis();
        CompanyEntity entity = new CompanyEntity();
//        判断是否有数据
        boolean isHave = jedis.exists("logo");
        if (isHave) {
// 将redis里面的数据取出来,放入entity
            entity.setLogo(jedis.get("logo"));
            entity.setPhone(jedis.get("phone"));
            entity.setNet(jedis.get("net"));
            entity.setLinePhone(jedis.get("linePhone"));
            entity.setEmail(jedis.get("email"));
            entity.setAddress(jedis.get("address"));
            entity.setVersion(jedis.get("version"));
//            System.out.println("jedis");
        } else {
            //        将vo的值copy到entity
            CompanyEntity companyEntity = Tool.copy(homeVO, CompanyEntity.class);
//        将entity的值copy到dto
            entity = companyDao.find(companyEntity);
            /**
             * 将entity保存到redis
             */
            jedis.set("logo", entity.getLogo());
//            jedis.expire("logo", 24 * 60 * 60 * 6);
//            jedis.setex("logo", 24 * 60 * 60 * 6,entity.getLogo());
            jedis.set("phone", entity.getPhone());
            jedis.set("email", entity.getEmail());
            jedis.set("net", entity.getNet());
            jedis.set("address", entity.getAddress());
            jedis.set("linePhone", entity.getLinePhone());
            jedis.set("version", entity.getVersion());
//            System.out.println("mysql");
        }
        return Tool.copy(entity, CompanyDTO.class);
    }
}
